package com.sunday.scoremanager.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;


import com.igexin.sdk.PushManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.entity.Member;
import com.sunday.scoremanager.push.PushIntentService;
import com.sunday.scoremanager.push.PushService;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by 刘涛 on 2017/10/26.
 */

public class BaseApp extends Application implements Thread.UncaughtExceptionHandler {
    private static BaseApp instance;

    public synchronized static BaseApp getInstance() {
        return instance;
    }


    private Member member;

    //public LocationService locationService;

    private Boolean isCanUpload = true;
    private boolean isLogin;
    private int payType;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        if (getCurProcessName(this) != null && getCurProcessName(this).equals("com.sunday.scoremanager")) {
            /***
             * 初始化定位sdk，建议在Application中创建
             */
            UMShareAPI.get(getApplicationContext());
            Thread.setDefaultUncaughtExceptionHandler(this);
            PlatformConfig.setWeixin(Constants.APP_ID, Constants.APPSecret);
            Config.DEBUG = true;
            Config.isJumptoAppStore = true;
            PushManager.getInstance().initialize(getApplicationContext(), PushService.class);
            PushManager.getInstance().registerPushIntentService(getApplicationContext(), PushIntentService.class);
        }

    }

    private String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        e.printStackTrace();
        System.exit(0);
    }


    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }


    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color._f5f4f9, R.color._666);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }


}